package com.duoketech.utils;

import com.duoketech.model.activity.Activity;
import com.duoketech.model.activityorder.ActivityOrder;
import com.duoketech.model.article.Article;
import com.duoketech.model.cart.Cart;
import com.duoketech.model.custom.Customization;
import com.duoketech.model.deliveryconfig.DeliveryConfig;
import com.duoketech.model.distribute.FoodDistribute;
import com.duoketech.model.member.Member;
import com.duoketech.model.memberinfo.MemberInfo;
import com.duoketech.model.mfood.MemberFood;
import com.duoketech.model.modulepic.ModulePic;
import com.duoketech.model.order.Order;
import com.duoketech.model.orderitem.OrderItem;
import com.duoketech.model.party.Party;
import com.duoketech.model.product.Product;
import com.duoketech.model.receiver.Receiver;
import com.duoketech.model.room.UnitRoom;
import com.duoketech.model.roomorder.RoomOrder;
import com.duoketech.model.unit.Unit;
import com.jfinal.kit.PropKit;
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.plugin.druid.DruidPlugin;
import com.jfinal.plugin.redis.Redis;

import java.util.List;

public class DbUtil {

    public static final String REDIS_CACHE_TRAVEL="Travel";

    public static void mappingModel(ActiveRecordPlugin arp){
        arp.addMapping(Article.dao.tableName(),Article.class);
        arp.addMapping(Activity.dao.tableName(),Activity.class);
        arp.addMapping(Order.dao.tableName(),Order.class);
        arp.addMapping(UnitRoom.dao.tableName(),UnitRoom.class);
        arp.addMapping(Unit.dao.tableName(),Unit.class);
        arp.addMapping(Member.dao.tableName(),Member.class);
        arp.addMapping(Customization.dao.tableName(),Customization.class);
        arp.addMapping(MemberFood.dao.tableName(),MemberFood.class);
        arp.addMapping(FoodDistribute.dao.tableName(),FoodDistribute.class);
        arp.addMapping(RoomOrder.dao.tableName(),RoomOrder.class);
        arp.addMapping(ActivityOrder.dao.tableName(),ActivityOrder.class);
        arp.addMapping(Receiver.dao.tableName(),Receiver.class);
        arp.addMapping(MemberInfo.dao.tableName(),MemberInfo.class);
        arp.addMapping(Product.dao.tableName(),Product.class);
        arp.addMapping(Cart.dao.tableName(),Cart.class);
        arp.addMapping(OrderItem.dao.tableName(),OrderItem.class);
        arp.addMapping(ModulePic.dao.tableName(),ModulePic.class);
        arp.addMapping(DeliveryConfig.dao.tableName(),DeliveryConfig.class);
        arp.addMapping(Party.dao.tableName(),Party.class);
    }

    public static void initDb(){
        PropKit.use("a_little_config.txt");
        DruidPlugin druidPlugin = new DruidPlugin(PropKit.get("jdbcUrl"),
                PropKit.get("user"), PropKit.get("password").trim());
        ActiveRecordPlugin arp = new ActiveRecordPlugin(druidPlugin);
        mappingModel(arp);
        druidPlugin.start();
        arp.start();
    }



    public static void initSchemaInfoDb(){

        String jdbcUrl="jdbc:mysql://127.0.0.1:3306/information_schema?characterEncoding=utf8&zeroDateTimeBehavior=convertToNull";
        String user="root";
        String password="";
        DruidPlugin druidPlugin = new DruidPlugin(jdbcUrl,user,password);
        ActiveRecordPlugin arp = new ActiveRecordPlugin(druidPlugin);
        druidPlugin.start();
        arp.start();
    }

    public static List<Record> queryColumnBy(String tableName){
        String sql="select * from COLUMNS where TABLE_NAME =?";
        return Db.find(sql,tableName);
    }

    public static String getMemberKey (int memberId){
        String memberKey = "MEMBER_"+memberId;
        return memberKey;
    }

    public static String getMemberShopKey (int memberId){
        String memberKey = "MEMBER_SHOP"+memberId;
        return memberKey;
    }

    public static Member getMember(int memberId){
        String memberKey = getMemberKey(memberId);
        Member member = Redis.use(REDIS_CACHE_TRAVEL).get(memberKey);
        return member;
    }

    public static Member getShopMember(int memberId){
        String memberKey = getMemberShopKey(memberId);
        Member member = Redis.use(REDIS_CACHE_TRAVEL).get(memberKey);
        return member;
    }

    public static void setMember(Member member){
        String memberKey = getMemberKey(member.getInt(Member.ID));
        Redis.use(REDIS_CACHE_TRAVEL).set(memberKey,member);
    }

    public static void setMemberShop(Member member){
        String memberKey = getMemberShopKey(member.getInt(Member.ID));
        Redis.use(REDIS_CACHE_TRAVEL).set(memberKey,member);
    }

    public static void removeMember(int memberId){
        String memberKey = getMemberKey(memberId);
        Redis.use(REDIS_CACHE_TRAVEL).del(memberKey);
    }

    public static void removeMemberShop(int memberId){
        String memberKey = getMemberShopKey(memberId);
        Redis.use(REDIS_CACHE_TRAVEL).del(memberKey);
    }
}

